package com.example.backspring.controller;

import com.example.backspring.Service.GoalsService;
import com.example.backspring.config.Message;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.models.annotations.OpenAPI30;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "目标管理", description = "增加、删除、修改目标数据")
@CrossOrigin(origins = "*")
@Slf4j
@RestController
@RequestMapping("/goalscon")
public class GoalController {
    @Resource
    private GoalsService goalService;

    /**
     * 增加目标
     *
     * @return
     */
    @Operation(summary = "增加目标", description = "添加目标数据")
    @PostMapping("/add")
    public Message addGoal(String goal) {
        Message res = goalService.addGoal(goal);
        return res;
    }

    @Operation(summary = "获取目标", description = "获取目标数据")
    @PostMapping("/getData")
    public Message getGoal() {
        Message res = goalService.getGoals();
        return res;
    }

    @Operation(summary = "更新目标", description = "更新目标数据")
    @PostMapping("/update")
    public Message updateGoal(Integer id, String lie, String context) {
        log.info("update id:{} lie:{} context:{}", id, lie, context);
        Message result = new Message();
        if (context == null || context.trim().isEmpty()) { // 判断 context 是否为空或仅包含空格
            result.setMessage("请输入数据"); // 设置返回信息
            result.setSuccess(false); // 设置请求不成功
            result.setStatus(0); // 设置状态
            return result;
        }
        if ("nr".equals(lie) && "删除".equals(context)) {
            result.setMessage("更新成功");
            result.setSuccess(true);
            result.setStatus(1);
            return result;
        }
        // 进行更新操作
        result = goalService.updateGoal(id, lie, context);
        return result;
    }
}
